﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MxWeiXinPF.DBUtility;

namespace MxWeiXinPF.DAL
{
    public partial class wxExecution
    {
        public int AddRecord(int wid)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("if not exists(select * from dt_timeExecution where wid=@wid and CONVERT(CHAR(10),ExecutionDate,23) = CONVERT(CHAR(10),getdate(),23))");
            builder.Append("begin \tinsert into dt_timeExecution(wid,isExecution) values (@wid,'1');select @@IDENTITY end");
            builder.Append("else begin \tselect 0 end");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@wid", SqlDbType.Int, 4) };
            cmdParms[0].Value = wid;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Exists(int wid)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from dt_timeExecution");
            builder.Append(" where wid=@wid and CONVERT(CHAR(10),ExecutionDate,23) = CONVERT(CHAR(10),getdate(),23)\t");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@wid", SqlDbType.Int, 4) };
            cmdParms[0].Value = wid;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }
    }
}
